import axios from "axios";
import type { AxiosResponse, AxiosRequestConfig } from "axios";
import { IResponse } from "@/types/index";

interface RequestList {
  [key: string]: () => void;
}

const request = axios.create({
  baseURL: "http://127.0.0.1:3000",
  timeout: 20000,
});

request.interceptors.request.use((config: any) => {
  const method = config.method?.toUpperCase();
  const data = config.data;
  if (method === "UPLOAD") {
    config["body"] = data;
    config["method"] = "POST";
    config["headers"]["Accept"] = "*/*";
    config["headers"]["x-provider"] = "set";
    delete config["headers"]["Content-type"];
    config["headers"]["Content-type"] = "multipart/form-data";
  }
  return config;
});

request.interceptors.response.use(
  (res) => {
    const { status } = res;
    if (status === 200) {
      return res.data;
    }
  },
  (error) => {
    const { response } = error;
    if (response) {
      const { data } = response;
      // message.error(data.error.message);
      return Promise.reject(data.error);
    }
    // message.error('服务器错误');
    return Promise.reject(error);
  }
);

function MyRequest<T>(params: AxiosRequestConfig): Promise<T> {
  return request(params);
}

export default MyRequest;
